package com.xnfh.home.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.xnfh.framework.base.BaseActivity
import com.xnfh.home.R
import com.xnfh.home.databinding.ActivityAgriculturalGlLayoutBinding
import com.xnfh.home.ui.fragment.AgriculturalGlFragment

/**
 * @Description:农资管理
 * @Author: ziheng
 * @CreateDate: 2023/12/28 17:35
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/12/28 17:35
 * @UpdateRemark: 更新说明
 */
class AgriculturalGlActivity : BaseActivity<ActivityAgriculturalGlLayoutBinding>() {
    override fun getLayoutID(): Int = R.layout.activity_agricultural_gl_layout

    companion object {
        fun start(context: Context, bundle: Bundle? = null) {
            val intent = Intent(context, AgriculturalGlActivity::class.java)
            bundle?.let { intent.putExtra("bundle", bundle) }
            context.startActivity(intent)
        }
    }

    private var mTitles: Array<String>? = null
    private var adapter: MyPagerAdapter? = null
    private val mFragments: ArrayList<Fragment> = ArrayList()

    override fun init(savedInstanceState: Bundle?) {
        setHeaderTitle(resources.getString(R.string.srt_nzgl))
        mTitles = arrayOf(
            resources.getString(R.string.str_nzgl_hf),
            resources.getString(R.string.str_nzgl_ny),
            resources.getString(R.string.str_nzgl_zz),
            resources.getString(R.string.str_nzgl_rg)
        )
        mTitles!!.forEach {
            //(0-农药 1化肥 2-种子 3-人工 4全部)
            val type: Int = when (it) {
                resources.getString(R.string.str_nzgl_hf) -> {
                    1
                }

                resources.getString(R.string.str_nzgl_ny) -> {
                    0
                }

                resources.getString(R.string.str_nzgl_zz) -> {
                    2
                }

                else -> {
                    3
                }
            }
            mFragments.add(AgriculturalGlFragment.getInstance(type))
        }
        adapter = MyPagerAdapter(mTitles!!, mFragments, supportFragmentManager)
        mBind.nzglViewpage.adapter = adapter
        mBind.nzglTablayout.setViewPager(mBind.nzglViewpage)

        //键盘的搜索按钮
        mBind.nzglEdit.setOnEditorActionListener(TextView.OnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                mFragments.forEach {
                    (it as AgriculturalGlFragment).setKeyWord(mBind.nzglEdit.text.toString())
                }
                val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
                imm.hideSoftInputFromWindow(v.windowToken, 0) //强制隐藏键盘
                return@OnEditorActionListener true
            }
            false
        })
        mBind.nzglEdit.setOnDeleteClickListener {
            mFragments.forEach {
                (it as AgriculturalGlFragment).setKeyWord(null)
            }
        }
    }

    override fun observe() {
    }

    private class MyPagerAdapter(
        val mTitles: Array<String>,
        val mFragments: ArrayList<Fragment>,
        supportFragmentManager: FragmentManager
    ) : FragmentPagerAdapter(supportFragmentManager) {

        override fun getCount(): Int {
            return mFragments.size
        }

        override fun getPageTitle(position: Int): CharSequence {
            return mTitles[position]
        }

        override fun getItem(position: Int): Fragment {
            return mFragments[position]
        }
    }
}